package com.xkdx.guangguang.fragment.my.setup;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.sql.NotifyDataHelper;
import com.xkdx.guangguang.sql.NotifyDataHelper.PushInfo;

public class NotifyFragment extends Fragment implements OnItemClickListener, OnLongClickListener {

	ListView listView;
	Button back;
	List<PushInfo> list;
	NotifyDataHelper helper;
	PushInfoAdapter pushInfoAdapter;
	View view;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		helper = new NotifyDataHelper(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.notify, container, false);
		findView();
		setonClick();
		return view;
	}

	private void findView() {
		listView = (ListView) view.findViewById(R.id.notify);
		back = (Button) view.findViewById(R.id.back_notify);
		list = helper.getAllPushInfo();

	}

	private void setonClick() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentManager fm = getFragmentManager();
				fm.popBackStack();
			}
		});

		listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

			@Override
			public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
				menu.add(0, 0, 0, "删除");
			}
		});

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		NotifyDataHelper notifyDataHelper = new NotifyDataHelper(getActivity());
		int selectPosition = ((AdapterContextMenuInfo) item.getMenuInfo()).position;
		notifyDataHelper.deletNotify(list.get(selectPosition).getMessage());
		list.remove(selectPosition);
		pushInfoAdapter.notifyDataSetChanged();

		return super.onContextItemSelected(item);
	}

	class PushInfoAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			viewHolder holder = null;
			if (convertView == null) {
				holder = new viewHolder();
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.notify_item, null);
				holder.tv = (TextView) convertView.findViewById(R.id.notify_message);
				holder.data = (TextView) convertView.findViewById(R.id.notify_time);
				holder.im = (ImageView) convertView.findViewById(R.id.notify_isRead);
				convertView.setTag(holder);
			} else {
				holder = (viewHolder) convertView.getTag();
			}
			holder.tv.setText(list.get(position).getMessage());
			holder.data.setText(list.get(position).getData());
			if (list.get(position).getIsRead().equals("false")) {
				holder.im.setImageResource(R.drawable.notify_not_read);
			} else {
				holder.im.setImageResource(R.drawable.notify_isread);
			}
			return convertView;
		}

		class viewHolder {
			ImageView im;
			TextView tv;
			TextView data;
		}

	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		helper.Close();
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		helper.upDateNotifyToisRead(list.get(position));

	}
}
